﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using FundRaising.BLL;
using FundRaising.DAL;
using System.IO;
using System.Data;
using System.Data.SqlClient;




namespace FundRaising.GUI.Extensions
{
    public partial class RequestFiles : System.Web.UI.Page
    {
        RequestBLL BLL = new RequestBLL();
        DAL._FR_DS.RequestDataTable table = new _FR_DS.RequestDataTable();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            btnUploadFile.Click += new EventHandler(btnUploadFile_Click);
        }



        protected void Page_Load(object sender, EventArgs e)
        {

            table = BLL.GetRequestByKey();
            FundLabel.Text = Convert.ToString(table[0][3]);
            AssoLabel.Text = Convert.ToString(table[0][1]);
            DomainLabel.Text = Convert.ToString(table[0][2]);
            RequestKey12.Text = Convert.ToString(table[0][11]);
        }
        void btnUploadFile_Click(object sender, EventArgs e)
        {
            // exit if file-upload has no file
            if (!ctrFile.HasFile) return;

            // generate new fileId
            Guid fileId = Guid.NewGuid();
            Guid RequesrKeyGuid = new Guid(RequestKey12.Text);
          
            //RequestKey.Equals("E9701238-E4AF-4B35-B9B7-78410C72A726");
            // create insert query
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Eden-DatabaseConnectionString3"].ConnectionString);
                command.CommandText = @"insert into [RequestFile](FileId, FileName, FileType, FileSize, FileContent,  RequestKey,  FileInfo)
						values(@FileId, @FileName, @FileType, @FileSize, @FileContent,  @RequestKey, @FileInfo)";
                command.Parameters.Add("@FileId", SqlDbType.UniqueIdentifier).Value = fileId;
                command.Parameters.Add("@FileName", SqlDbType.NVarChar, 100).Value = Path.GetFileName(ctrFile.PostedFile.FileName);
                command.Parameters.Add("@FileType", SqlDbType.NVarChar, 100).Value = ctrFile.PostedFile.ContentType;
                command.Parameters.Add("@FileSize", SqlDbType.Int).Value = ctrFile.PostedFile.ContentLength;
                command.Parameters.Add("@RequestKey", SqlDbType.UniqueIdentifier).Value = RequesrKeyGuid;
                command.Parameters.Add("@FileInfo", SqlDbType.NVarChar, 100).Value = FileInfoTextBox.Text;
                // filecontent, convert from stream to byte array
                byte[] fileContent = new byte[ctrFile.PostedFile.ContentLength];
                ctrFile.PostedFile.InputStream.Read(fileContent, 0, ctrFile.PostedFile.ContentLength);
                command.Parameters.Add("@FileContent", SqlDbType.VarBinary, -1).Value = fileContent;
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            // show result
            ctrResult.NavigateUrl = "FileStream.ashx?FileId=" + fileId.ToString();
            ctrResult.Text = "Click here to view the uploaded file";
            Response.Redirect("RequestFiles.aspx"); 
        }


        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            EditRequest.Curr.TheSelectedFile = GridView1.SelectedValue.ToString();
            //Response.Redirect("FileStream.ashx?FileId=");
            string url = "FileStream.ashx?FileId=";
            string fullURL = "window.open('" + url + "', '_blank', 'height=500,width=800,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no' );";
            ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", fullURL, true);

        }

    }
}
